// import store from '@/store';
import { adMainTypeOptions } from '/@/views/marketing/posterList/hook';
import dayjs from 'dayjs';
import { storeToRefs } from 'pinia';
import { useOptions } from '/@/stores/options';

const storesOptions = useOptions();
const { cateList, positionList } = storeToRefs(storesOptions);

//时间
export const timeFormat = (time, separator) => {
	if (!time) return '';
	// 如果存在分隔符
	if (separator) {
		// 如果分隔符为text，则返回中文年月日
		if (separator === 'text') {
			return dayjs(time).format('YYYY年MM月DD日');
		} else if (separator === 'ALL') {
			return dayjs(time).format('YYYY年MM月DD日 HH:mm:ss');
		} else {
			return dayjs(time).format('YYYY' + separator + 'MM' + separator + 'DD');
		}
	} else {
		return dayjs(time).format('YYYY-MM-DD HH:mm:ss');
	}
};
//数值绝对值
export const abs = (num) => {
	if (num) {
		return Math.abs(num);
	} else {
		return 0;
	}
};
//商品上架状态
export const publishStatusFormat = (status) => {
	if (status === 1) {
		return '上架';
	} else if (status === 0) {
		return '下架';
	} else {
		return '-';
	}
};

// //商品分类format
export const cateFormat = (id) => {
	function findName(array) {
		let _name = '';
		for (let index = 0; index < array.length; index++) {
			const element = array[index];
			if (element.id === id) {
				_name = element.name;
				break;
			}
			if (element.children) {
				_name = findName(element.children);
				if (_name) break;
			}
		}
		return _name;
	}
	console.log(cateList.value);
	const res = findName(cateList.value);
	return res || '-';
};
//广告位位置;
export const positionFormat = (id) => {
	const value = positionList.value.find((item) => item.id === id) || {};
	return value.positionName || '-';
};
//广告主体类型;
export const adMainTypeFormat = (type) => {
	const value = adMainTypeOptions.find((item) => item.value === type) || {};
	return value.label || '-';
};
//站点：EU->德国；UK->英国；US->美国
export const siteFormat = (site) => {
	let siteName = '';
	switch (site) {
		case 'EU':
			siteName = '德国';
			break;
		case 'UK':
			siteName = '英国';
			break;
		case 'US':
			siteName = '美国';
			break;
		default:
			siteName = '-';
			break;
	}
	return siteName;
};
export const priceFormat = (price) => {
	return Number(price).toFixed(2);
};
